package com.starter.home.controller;


import com.starter.home.domain.entity.User;
import com.starter.home.service.IUserService;
import com.starter.home.util.CommonParam;
import com.starter.home.util.MyUtil;
import com.starter.home.util.PageUtil;
import com.starter.home.util.ResponseResult;
import com.starter.home.util.constant.MessageConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2020-04-29
 */
@RestController
@RequestMapping("/user")
@ControllerAdvice
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户登录 存储 session 去除pwd返回前端
     * @param user
     * @param session
     * @return
     */
    @PostMapping(value = "/login")
    public ResponseResult login(@RequestBody User user, HttpSession session){
        User user1 = userService.toLogin(user);
        session.setAttribute("user",user1);
        user1.setPassword("");
        return ResponseResult.success(user1);
    }


    /**
     * 更改状态
     * @return
     */
    @PostMapping(value = "/changeStatus/{id}/{status}")
    public ResponseResult changeStatus(@PathVariable Integer id,@PathVariable Boolean status){
        userService.changeStatus(id,status);
        return ResponseResult.success(null);
    }

    /**
     * 删除用户
     * @return
     */
    @PostMapping(value = "/delete/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        userService.deleteById(id);
        return ResponseResult.success(null);
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping(value = "/getInfo")
    public ResponseResult getInfo(HttpSession session){
        Optional<User> user = MyUtil.getUser(session);
        if (!user.isPresent()){
            return ResponseResult.error("会话过期，请您重新登录!");
        }
        User userNew = userService.getBaseMapper().selectById(user.get().getId());
        userNew.setPassword("");
        return ResponseResult.success(userNew);
    }



    /**
     * 查找用户
     * @return
     */
    @GetMapping(value = "/getList")
    public ResponseResult getList(@ModelAttribute CommonParam commonParam,HttpSession session){
        Optional<User> user = MyUtil.getUser(session);
        if (user.isPresent()){
            PageUtil pageUtil = userService.getUserList(commonParam,user.get().getId());
            return ResponseResult.success(pageUtil);
        }else{
            return ResponseResult.error(MessageConstant.SESSION_OUTTIME);
        }
    }
    @GetMapping(value = "/getUserList")
    public ResponseResult getList(@ModelAttribute CommonParam commonParam){

        PageUtil pageUtil = userService.getUserAllList(commonParam);
        return ResponseResult.success(pageUtil);
    }

    /**
     * 推荐用户
     * @param session
     * @return
     */
    @GetMapping(value = "/getUserRecommend")
    public ResponseResult getUserRecommend(HttpSession session){
        Optional<User> user = MyUtil.getUser(session);
        if (user.isPresent()){
            List<User> users = userService.getBaseMapper().selectList(null);
            return ResponseResult.success(users);
        }else{
            return ResponseResult.error(MessageConstant.SESSION_OUTTIME);
        }
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping(value = "/register")
    public ResponseResult register(@RequestBody User user){
        //检验重复账户的注册
        userService.findByAccount(user.getAccount());
        user.setAvatar("https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3701847413,2165986719&fm=111&gp=0.jpg");

        boolean save = userService.save(user);
        if (save){
            return ResponseResult.success(MessageConstant.REGISTER_SUCCESS);
        }else{
            return ResponseResult.error(MessageConstant.REGISTER_ERROR);
        }
    }
    @PostMapping(value = "/updateUser")
    public ResponseResult updateUser(@RequestBody User user){
        //检验重复账户的注册
       boolean result = userService.updateUser(user);
       if (result){
           user.setPassword("");
           return ResponseResult.success(user);
       }
       return ResponseResult.error("更新失败!");
    }

}
